package com.burial.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burial.common.log.MyLog;
import com.burial.common.result.Result;
import com.burial.dto.SaveOrderDTO;
import com.burial.entity.Order;
import com.burial.entity.PageBean;
import com.burial.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/order")
@Api(tags = "订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    // 修改订单状态
    @ApiOperation("更新订单状态")
    @GetMapping("updateState/{id}/{state}")
    @MyLog(type = "订单信息的更新",msg = "更新订单的状态")
    public Result updateState(@PathVariable Long id,
                              @PathVariable Integer state){
        orderService.updateState(id,state);
        return Result.success();
    }

    // 根据ID查询订单详细信息
//    @ApiOperation("根据ID查询订单详情")
//    @GetMapping("getInfo/{oid}")
//    public Result getOrderInfoById(@PathVariable Long oid){
//        OrderDTO orderInfo = orderService.getOrderInfoById(oid);
//        return Result.success();
//    }

    // 订单分页查询
    @GetMapping("{page}/{limit}")
    @ApiOperation("订单分页查询")
    @MyLog(type = "订单信息的检索",msg = "分页检索所有订单信息")
    public Result pageOrder(@PathVariable Integer page,
                            @PathVariable Integer limit){
        PageBean pageBean = orderService.pageOrder(page,limit);
        return Result.success(pageBean);
    }
//    @GetMapping("{page}/{limit}")
//    @ApiOperation("订单分页查询")
//    public Result pageQueryOrder(@PathVariable Integer page,
//                                 @PathVariable Integer limit){
//        Page<Order> pageParam = new Page<>(page,limit);
//        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<Order>().ne(Order::getState,-1);
//        orderService.page(pageParam, wrapper);
//        return Result.success(pageParam);
//    }
    // 添加订单
    @ApiOperation("添加订单")
    @PostMapping("save")
    @MyLog(type = "订单信息的更新",msg = "添加一条订单")
    public Result save(@RequestBody SaveOrderDTO orderinfo){
        if (orderinfo==null){
            return Result.error("输入信息出错");
        }
        boolean ret = orderService.saveOrder(orderinfo);
        if (!ret){
            return Result.error("添加出错");
        }
        return Result.success();
    }
//    public Result save(@RequestBody Order order){
//        orderService.save(order);
//        return Result.success();
//    }
    // 修改订单
    @ApiOperation("根据ID查询订单")
    @GetMapping("get/{id}")
    @MyLog(type = "订单信息的更新",msg = "根据id查询订单")
    public Result getOrderById(@PathVariable Long id){
        if(id==null){
            return Result.error("输入出错");
        }

        Order order = orderService.getById(id);

        if (order==null){
            return Result.error("查询出错");
        }
        return Result.success(order);
    }
    @ApiOperation("修改订单")
    @PutMapping("update")
    @MyLog(type = "订单信息的更新",msg = "修改了订单的信息")
    public Result updateById(@RequestBody Order order){
        if (order==null){
            return Result.error("输入信息出错");
        }
        boolean ret = orderService.updateById(order);
        if (!ret){
            return Result.error("修改订单信息出错");
        }
        return Result.success();
    }
    // 删除订单
    @ApiOperation("删除订单")
    @DeleteMapping("remove/{id}")
    @MyLog(type = "订单信息的更新",msg = "删除一条订单信息")
    public Result remove(@PathVariable Long id){
        if (id==null){
            return Result.error("输入信息出错");
        }
        boolean ret = orderService.removeById(id);
        if (!ret){
            return Result.error("删除信息出错");
        }
        return Result.success();
    }
}

